package com.example.mask.maskmobilesshop.controller;


import com.example.mask.maskmobilesshop.enums.HttpStatus;
import com.example.mask.maskmobilesshop.service.AttritubeService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.crypto.hash.Hash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 属性的Controller
 */
@RestController
@RequestMapping(value = "/attritube")
public class AttritubeController {

    private Logger logger = LoggerFactory.getLogger(LoginController.class.getName());


    @Autowired
    private AttritubeService attritubeService;


    /**
     *
     * @param type
     * @return  result
     */
    @ApiOperation(value = "根据类型获取商品属性", notes = "属性类型:1-SKU属性 2-基本属性 3-公共属性 4-评论属性 5-查询属性")
    @ApiImplicitParam(name = "type", value = "属性类型", required = true, dataType = "String", paramType = "query")
    @RequestMapping(value = "/getAttritubeList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HashMap<String, Object> getAttritubeList(String type) {
        HashMap<String, Object> result = new HashMap<>();
        try {
            if (!StringUtils.isEmpty(type)) {
                List<Map<String, Object>> attritubeList = attritubeService.findByType(Integer.parseInt(type));
                if (attritubeList != null && attritubeList.size() > 0) {
                    result.put("result", attritubeList);
                    result.put("status", HttpStatus.SUCCESS.getValue());

                }
            }
            result.put("status", HttpStatus.BAD_PARAMETERS.getValue());
        } catch (Exception e) {
            result.put("status", HttpStatus.INTERNAL_SERVER_ERROR.getValue());
            logger.error("获取属性列表发生了异常，错误异常如下:");
            e.printStackTrace();

        }
        return result;
    }

}
